package com.vip.pathbootstarter.anon;

import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
public class JacksonConfig {

    @Bean
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder, PathFieldAnnotationIntrospect introspect) {
        // 根据已有的配置创建自定义的ObjectMapper
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        AnnotationIntrospector annotationIntrospector = objectMapper.getSerializationConfig().getAnnotationIntrospector();
        // 将自定义注解內省器加入到Jackson注解内省器集合里，AnnotationIntrospect是双向链表结构
        AnnotationIntrospector pair = AnnotationIntrospectorPair.pair(annotationIntrospector, introspect);
        objectMapper.setAnnotationIntrospector(pair);
        return objectMapper;
    }

    @Bean
    public PathFieldAnnotationIntrospect urlFieldAnnotationIntrospect() {
        return new PathFieldAnnotationIntrospect();
    }

    /**
     * 序列化
     * @return
     */
    @Bean
    public PathFieldSerializer urlFieldSerializer() {
        return new PathFieldSerializer();
    }

    /**
     * 反序列化
     * @return
     */
    @Bean
    public PathFieldDeserializer urlFieldDeserializer() {
        return new PathFieldDeserializer();
    }

}

